// Armisael.cpp : Defines the entry point for the console application.
//

// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual C++ 2010 Express and OpenCV 2.1.0


// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <highgui.h>

using namespace cv;

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

IplImage *Igray=0, *It=0, *Iat, *thresh2;


int main(int argc, char** argv)
{
	
	//NSLog(@"Total is : %i",	argc);
	std::cout << "argc: " << argc << std::endl;
	//if(argc != 7) return -1;
	
	//Command line 
	double threshold = (double)atof(argv[1]);
	
	int threshold_type = atoi(argv[2]) ? CV_THRESH_BINARY :  CV_THRESH_BINARY_INV;
	

	int adaptive_method = atoi(argv[3])	?
		CV_ADAPTIVE_THRESH_MEAN_C :CV_ADAPTIVE_THRESH_GAUSSIAN_C;
	
	
	int block_size = atoi(argv[4]);
	double offset = (double)atof(argv[5]);
	
	//Read in gray image
	if((Igray = cvLoadImage(argv[6], CV_LOAD_IMAGE_GRAYSCALE))==0)
		return -1;
		//8uC1 
	//Create the graysclae output images
		It = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U,1);
		Iat = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U,1);
		thresh2 = cvCreateImage(cvSize(Igray->width, Igray->height),IPL_DEPTH_8U,1);
	
											
	//Treshold
	
	
	cvThreshold(Igray, It, threshold, 255, threshold_type);
	cvAdaptiveThreshold(Igray, Iat, 255, adaptive_method, threshold_type, block_size, offset);
	std::vector<cv::Vec4i> hierarchy2;

	//Find countours
	std::vector<std::vector<cv::Point> > contours2;


	//CvMemStorage* storage = NULL;
	//CvPoint Offset = cvPoint (0,0);



	Scalar          color       = CV_RGB(255,255,255); // text color
    //IplImage*       BprojectIpl =  &IplImage(Bproject);
    CvMemStorage*   storage     = cvCreateMemStorage(0);
    CvSeq*          contours    = 0;
    int             numCont     = 0;
    int             contAthresh = 45;

    numCont= cvFindContours( It, storage, &contours, sizeof(CvContour),
                    CV_RETR_TREE, CV_CHAIN_APPROX_NONE, cvPoint(0,0) );

    
	std::cout << "numCont: " << numCont << std::endl;


	//CvSeq* contours;
	//cvFindContours( It, storage, &contours,88,1,2,Offset);
	//cvZero( It );
//	thresh2,contours2,hierarchy2,CV_RETR_TREE, CV_CHAIN_APPROX_NONE );
	//findContours(thresh2,contours2,hierarchy2,CV_RETR_TREE, CV_CHAIN_APPROX_NONE,);

		//( thres2 , contours2, hierarchy2,CV_RETR_TREE, CV_CHAIN_APPROX_NONE );
	

	//Put TWO windows
	cvNamedWindow("Raw", 1);
	cvNamedWindow("Threshold", 1);
	cvNamedWindow("Adaptive Threshold", 1);
	
	//Show Results
	cvShowImage("Raw", Igray);
	cvShowImage("Thresh", It);
	cvShowImage("Adaptive Threshold", Iat);
	
	cvWaitKey(0);
	
	//Clean up
	cvReleaseImage(&Igray);
	cvReleaseImage(&It);
	cvReleaseImage(&Iat);
	
	cvDestroyWindow("Raw");
	cvDestroyWindow("Threshold");
	cvDestroyWindow("Adaptive Threshold");
	
	return (0);	
}
